package com.example.program.controller;

import com.example.program.anno.LOg;
import com.example.program.pojo.Emp;
import com.example.program.pojo.PageBean;
import com.example.program.pojo.Result;
import com.example.program.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;
@GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,

                       @RequestParam (defaultValue = "10") Integer pageSize,
                       String name, Short gender, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end
                       )
{

    PageBean pageBean=empService.page(page,pageSize,name,gender,begin,end);

    return Result.success(pageBean);

}
@LOg
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids)
    {
        empService.delete(ids);

        return Result.success();
    }
@PostMapping
@LOg
    public Result save(@RequestBody Emp emp)
{
empService.save(emp);

    return Result.success();
}

//本地磁盘保存
@PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
    String originalFilename=image.getOriginalFilename();
    //构建文件名
    String extname=originalFilename.substring(originalFilename.lastIndexOf("."));
    String filename= UUID.randomUUID().toString()+extname;
    image.transferTo(new File("H:\\Program\\filename\\"+filename));
    return Result.success();

}
//修改员工
@GetMapping("/{id}")
@LOg
    public Result getByid(@PathVariable Integer id)
{

Emp emp=empService.getByid(id);
    return Result.success(emp);
}
//
    @PutMapping
    @LOg
    public Result update(@RequestBody Emp emp)
    {
        empService.update(emp);
return Result.success();
    }
}
